package com.wx.login
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log

class WXLoginBroadcast(private val broadcastCallback: BroadcastCallback?) : BroadcastReceiver() {

    companion object {
        const val BROADCAST_ACTION_SUCCESS = ".WXLoginBroadcast.success"

        const val BROADCAST_ACTION_FAILED = ".WXLoginBroadcast.failed"

        const val KEY_WX_CODE = "key_wx_code"

        const val TAG = "WXLoginBroadcast"
    }

    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action
//        Log.e(TAG, "微信登录action:$action")
        if (action == "${context.packageName}$BROADCAST_ACTION_SUCCESS") {
            val wxCode = intent.getStringExtra(KEY_WX_CODE)
            Log.e(TAG, "微信登录成功:$wxCode")
            broadcastCallback?.onSuccess(wxCode)
        } else if (action == "${context.packageName}$BROADCAST_ACTION_FAILED") {
            Log.e(TAG, "微信登录失败")
            broadcastCallback?.onFailed()
        }
    }

    interface BroadcastCallback {
        fun onSuccess(code: String?)
        fun onFailed()
    }
}